class Solution {
    public int[] constructArr(int[] a) {
        if(a.length == 0 || a.length == 1) {
            return a;
        }
        if(a.length == 2) {
            return new int[]{a[1],a[0]};
        }
        int length = a.length;
        int[] left = new int[length];
        left[0] = a[0];
        for(int i = 1; i < length;i++) {
            left[i] = a[i] * left[i-1];
        }

        int[] rigth = new int[length];
        rigth[length-1] = a[length-1];
        for(int i = length-2; i >= 0;i--) {
            rigth[i] = a[i] * rigth[i+1];
        }

        int[] s = new int[length];
        for(int i = 0 ;i < length;i++) {
            if(i == 0) {
                s[i] = rigth[i+1];
            }else if(i == (length-1)) {
                s[i] = left[i-1];
            } else{
               s[i] = left[i-1]*rigth[i+1]; 
            }
        }
        return s;
    }
}